package com.tangdi.mkfile.bank_bankcc;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.tangdi.Utils;
import com.tangdi.Bean.CZGZ_DWXX_TOTAL;
import com.tangdi.Bean.CZGZ_ZGGZMX;
import com.tangdi.Dao.impl.GongziBankInfoImpl;
import com.tangdi.Dao.impl.GongziDWImpl;
import com.tangdi.mkfile.gzdwhz.CreateDWGZExcel_LISTALL;

public class CreateBankTxt {
	private Logger logger = Logger.getLogger(CreateDWGZExcel_LISTALL.class);
	 public String file_url = Utils.getFilePath("root_path");
	 private String file_Type = "txt";
	 private DecimalFormat decimalFormat=new DecimalFormat("#0.00");
	 
	 public int writeFile(String month){
		 
		 String filePath = file_url  + month + File.separator + file_Type + File.separator + month + "_bank.txt";
	        try {
	            File file = new File(filePath); //相对路径，如果没有则要建立一个新的bankcc.txt文件
	            String tab = "\t\t";
	            String enter = "\r\n"; // \r\n即为换行
	            String blank = "      ";
	            String blank2 = "  ";
	            if(!file.exists()){
	   			 //先得到文件的上级目录，并创建上级目录，再创建文件
	            	file.getParentFile().mkdirs();
	            	file.createNewFile();
	   			}
	            
	            FileWriter writer = new FileWriter(file);
	            StringBuffer write_str = new StringBuffer(); 
	            BufferedWriter out = new BufferedWriter(writer);
	            GongziBankInfoImpl gzbank = new GongziBankInfoImpl();
	            
/*	            	统发汇总表
					1.电汇：CZGZ_ZGGZMX.PER_AC='2'
					2.无账号：CZGZ_ZGGZMX.PER_AC='' or CZGZ_ZGGZMX.PER_AC=null
					3.实发为0：CZGZ_ZGGZMX.TXN_AMT='0.00' or CZGZ_ZGGZMX.TXN_AMT=null
					4.实际代发：CZGZ_ZGGZMX.DTL_STS='S'
					说明：
					1.当账户为2时，为电汇明细
					2.当账户为空或空字符时，为无账号
					3.当金额为0或空时，为实发为0
					4.交易状态为S时，为代发成功的明细，统计笔数和金额即为实际代发
*/	            
	            CZGZ_DWXX_TOTAL dfGZ_Total=new CZGZ_DWXX_TOTAL();
				GongziDWImpl gzmx = new GongziDWImpl();
				dfGZ_Total = gzmx.getGZDWCount(month);
                Date sys_date = new Date(); 
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd");//可以方便地修改日期格式
				
	            String sf_amt = gzbank.getGZ_TOTAL(month, " AND DTL_STS = 'S'");
	            String sf_count = gzbank.getGZ_COUNT(month, " AND DTL_STS = 'S'");
	            
	            String wzh_amt = gzbank.getGZ_TOTAL(month, " AND (PER_AC = '' OR PER_AC = null)");
	            String wzh_count = gzbank.getGZ_COUNT(month, " AND (PER_AC = '' OR PER_AC = null)");
	            
	            String sf0_count = gzbank.getGZ_COUNT(month, " AND (TXN_AMT = '' OR TXN_AMT = '0.00' OR TXN_AMT = null)");
	            
	            String dh_amt = gzbank.getGZ_TOTAL(month, " AND PER_AC = '2'");
	            String dh_count = gzbank.getGZ_COUNT(month, " AND PER_AC = '2'");

	            //int sf_amt_js = Integer.valueOf(sf_amt) - Integer.valueOf(dh_amt) - Integer.valueOf(wzh_amt);
	            int sf_count_js = Integer.valueOf(sf_count) + Integer.valueOf(sf0_count) - Integer.valueOf(dh_count);
	            logger.info("sf_amt: " + sf_amt + " | " + "sf_count: " + sf_count + " | " + 
	            			"wzh_amt: " + wzh_amt + " | " +  "wzh_count: " + wzh_count + " | " + 
	            		    "sf0_count: " + sf0_count + " | " +  "sf_count_js: " + sf_count_js);
	            
	           //out.write(tab + tab + tab + tab + "bank");
               //out.write(enter);
               out.write(enter);
               
               out.write(tab + "交通银行南京分行  市财政工资统发汇总表");
               out.write(enter);
               out.write(enter);
               
               out.write(blank + "统发月份：" + month);
               out.write(enter);
               out.write(blank + "打印日期：" + dateFormat.format(sys_date));
               out.write(enter);
               out.write(enter);
               
               out.write(blank + appentStr4Length("应发工资",10,""));
               out.write("笔数：");
               out.write(appentStr4Length(dfGZ_Total.getTF_RS_TOTAL().replace(",", "").replace(".00", ""), 8, "r"));
               out.write(blank + "金额：");
               out.write(appentStr4Length(dfGZ_Total.getYF_GZ_TOTAL().replace(",", ""), 15, "r"));
               out.write(enter);
               
               out.write(blank + appentStr4Length("代扣工资",10,""));
               out.write("笔数：");
               out.write(appentStr4Length(dfGZ_Total.getTF_RS_TOTAL().replace(",", "").replace(".00", ""), 8, "r"));
               out.write(blank + "金额：");
               out.write(appentStr4Length(dfGZ_Total.getDF_GZ_TOTAL().replace(",", ""), 15, "r"));
               out.write(enter);
               
               out.write(blank + appentStr4Length("实发工资",10,""));
               out.write("笔数：");
               out.write(appentStr4Length(dfGZ_Total.getTF_RS_TOTAL().replace(",", "").replace(".00", ""), 8, "r"));
               out.write(blank + "金额：");
               out.write(appentStr4Length(dfGZ_Total.getSF_GZ_TOTAL().replace(",", ""), 15, "r"));
               out.write(enter);
               
               out.write(write_str.toString());
               out.write(blank + "---------------------------------------------------------------");
               out.write(enter);
               
               out.write(blank + appentStr4Length("电汇",10,""));
               out.write("笔数：");
               out.write(appentStr4Length(dh_count, 8, "r"));
               out.write(blank + "金额：");
               out.write(appentStr4Length(dh_amt, 15, "r"));
               out.write(enter);
               
               out.write(blank + appentStr4Length("无账号",10,""));
               out.write("笔数：");
               out.write(appentStr4Length(wzh_count, 8, "r"));
               out.write(blank + "金额：");
               out.write(appentStr4Length(wzh_amt, 15, "r"));
               out.write(enter);

               out.write(blank + appentStr4Length("实发为0",10,""));
               out.write("笔数：");
               out.write(appentStr4Length(sf0_count, 8, "r"));
               out.write(enter);
               
               out.write(blank + appentStr4Length("实际代发",10,""));
               out.write("笔数：");
               out.write(appentStr4Length(sf_count, 8, "r"));
               out.write(blank + "金额：");
               out.write(appentStr4Length(sf_amt, 15, "r"));
               out.write(enter);
               
               out.write(blank + "---------------------------------------------------------------");
               out.write(enter);
               
               out.write(blank + "应发工资 = 代扣金额 + 实发金额");
               out.write(enter);
               out.write(blank + "实际代发 = 实发金额 - 电汇金额 - 无账号金额");
               out.write(enter);
               out.write(blank + "反划5031 = 代扣金额 + 电汇金额 = " + decimalFormat.format(Double.parseDouble(dh_amt) + Double.parseDouble(dfGZ_Total.getDF_GZ_TOTAL().replace(",", ""))));
               out.write(enter);
               
               out.write(blank + "---------------------------------------------------------------");
               out.write(enter);
               out.write(blank + "★作市政财政工资统发储蓄2621结算户5031辖内往来传票附件。");               
               out.write(enter);
               out.write(blank + "私金处经办人员签字：♀");
               out.write(enter);
               out.write(enter);

               out.write(tab + tab +"交通银行南京分行  市财政工资统发电汇明细表");              
               out.write(enter);
               out.write(enter);

               out.write(blank2 + "统计月份：" + month);
               out.write(enter);
               out.write(blank2 + "打印日期：" + dateFormat.format(sys_date));
               out.write(enter);
               out.write(enter);
               
               out.write(blank2 + appentStr4Length("电汇",10,""));
               out.write("笔数：");
               out.write(appentStr4Length(dh_count, 8, "r"));
               out.write(blank + "金额：");
               out.write(appentStr4Length(dh_amt, 15, "r"));
               out.write(enter);
               
               List<CZGZ_ZGGZMX>  list_DianHui = new ArrayList<CZGZ_ZGGZMX>();
               list_DianHui = gzbank.getGZ_DianHui(month);
               
               out.write(blank2 + appentStr4Length("序号", 6, ""));
               out.write(blank2 + appentStr4Length("单位名称", 50, ""));
               out.write(appentStr4Length("姓名", 10, ""));
               out.write(appentStr4Length("实发金额", 15, ""));
               out.write(appentStr4Length("电汇账号", 25, ""));
               out.write(appentStr4Length("电汇地址", 20, ""));
               out.write(enter);
          	
               out.write(blank2 + "-------------------------------------------------------------------------------------------------------------------------------------------");
               out.write(enter);
              
                for(int i = 0; i < list_DianHui.size(); i++){
               	CZGZ_ZGGZMX mx = new CZGZ_ZGGZMX();
               	mx = list_DianHui.get(i);
               	out.write(blank2 + appentStr4Length((i + 1)+"", 6, ""));
               	out.write(blank2 + appentStr4Length(mx.getDW_MC(), 50, ""));
               	out.write(appentStr4Length(mx.getNME(), 10, ""));
               	out.write(appentStr4Length(mx.getSF_TXN_AMT(), 15, ""));
               	out.write(appentStr4Length(mx.getDHKH(), 25, ""));
               	out.write(appentStr4Length(mx.getDHDZ(), 20, ""));
               	out.write(enter);
               }
               
                out.write(blank2 + "-------------------------------------------------------------------------------------------------------------------------------------------");
                out.write(enter);
                
               out.write(blank2 + "私金处经办人员签字：");               
               out.write(enter);
               //out.write(write.toString().getBytes("UTF-8"));
               out.flush(); // 把缓存区内容压入文件
               out.close();
	            
	        } catch (IOException e) {
	            e.printStackTrace();
	            return -1;
	        }
	        return 0;
	    }

   public static String appentStr4Length(String str , int length, String align){
       if(str == null){
           str = "";
       }
       try {
           int strLen = 0;//计算原字符串所占长度,规定中文占两个,其他占一个
           for(int i = 0 ; i<str.length(); i++){
               if(isChinese(str.charAt(i))){
                   strLen = strLen + 2;
               }else{
                   strLen = strLen + 1;
               }
           }
           if(strLen>=length){
               return str;
           }
           int remain = length - strLen;//计算所需补充空格长度
           if("r".equals(align)){
           	for(int i =0 ; i< remain ;i++){
           		str = " " + str;
           	}
           }else{
           	for(int i =0 ; i< remain ;i++){
           		str = str + " ";
           	}
           }
       } catch (Exception e) {
           e.printStackTrace();
       }
       return str;
   }
   // 根据Unicode编码完美的判断中文汉字和符号
   private static boolean isChinese(char c) {
       Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
       if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
               || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
               || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
               || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
               || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
               || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
               || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
           return true;
       }
       return false;
   }
	
/*	public static void main(String[] args) {
		CreateBankTxt cb = new CreateBankTxt();
		String month = "201905";
		cb.writeFile(month);

	}*/

}
